﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using CNotebook;
using CNotebook.Inventory;

namespace NotebookS2
{
    public partial class M_ITEMCATEGORY : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public M_ITEMCATEGORY()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public DataTable CategoryTable { get; set; }

        void InitTable()
        {
            DataTable tbl = new DataTable();

            
            tbl.Columns.Add("DivisionCode", typeof(string));
            tbl.Columns.Add("OldCatCode", typeof(string));
            tbl.Columns.Add("CategoryCode", typeof(string));
            tbl.Columns.Add("CategoryName", typeof(string));

            this.CategoryTable = tbl;
        }
        void BindDivision(string sItemGroup)
        {
            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = oManager.GetItemDivision(sItemGroup);
            oManager.Close();

            cboDivision.Properties.DataSource = oTable;
            cboDivision.Properties.ValueMember = "DivisionCode";
            cboDivision.Properties.DisplayMember = "DivisionName";

            cboDivision.Properties.Columns.Clear();

            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DivisionName", "DivisionName", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboDivision.Properties.Columns.Add(col);
            cboDivision.Properties.ShowHeader = false;
            

            cboDivision.Refresh();

        }
        private void cboItemGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindDivision(cboItemGroup.Text);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            ItemManager oManager = new ItemManager();

            try
            {
                oManager.ConnectionString = this.ConnectionString;
                System.Data.SqlClient.SqlException ex = oManager.Open();

                oManager.UpdateItemCategory(this.CategoryTable);

                oManager.Close();

                if (ex == null)
                {
                    this.CategoryTable.AcceptChanges();
                    MessageBox.Show("Data is successfully updated!!!", "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(ex.Message.ToString(), "Update", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void M_ITEMCATEGORY_Load(object sender, EventArgs e)
        {
            cboItemGroup.SelectedIndex = 0;
            InitTable();

            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            this.CategoryTable = oManager.GetItemCategory(cboDivision.EditValue.ToString());

            oManager.Close();

            gridControl1.DataSource = this.CategoryTable;
            gridControl1.Refresh();
        }

        private void cboDivision_EditValueChanged(object sender, EventArgs e)
        {
            ItemManager oManager = new ItemManager();

            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();

            this.CategoryTable = oManager.GetItemCategory(cboDivision.EditValue.ToString());

            oManager.Close();

            gridControl1.DataSource = this.CategoryTable;
            gridControl1.Refresh();
        }

        private void gridView1_RowUpdated(object sender, DevExpress.XtraGrid.Views.Base.RowObjectEventArgs e)
        {
            gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DivCode"], cboDivision.EditValue.ToString());
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}